<?php
namespace app\index\controller;

use app\common\model\Gather as GatherModel;
use app\common\model\Gatherreg as GatherregModel;
/**
 * 义工管理控制器
 * Class Index
 * @package app\index\controller
 */
class Gather extends Base
{
    /**
     * [index  招募列表数据]
     * @return json
     * @author [C。]
     */
    public function getIndex1()
    {
        $param = input('param.');
        $user = $this->user;
        $map = [];
        $this->getPageAndSize($param);
        $model = new GatherModel;
        $rmodel = new GatherregModel;
        try{
            if(!empty($param['con'])){
                $map['title|name|phone'] = ['like', "%" . $param['con'] . "%"];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['starttime|endtime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $map['son_states'] = ['neq',-1];
            $map['comid'] = $user['comid'];
            $map['type'] = 2;
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size);
            foreach($lists as &$v){
                $regnum = $rmodel->getAllCount(['gather_id'=>$v['id']]);
                $v['starttime'] = date('Y-m-d',$v['starttime']);
                $v['endtime'] = date('Y-m-d',$v['endtime']);
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                $v['num'] = $regnum;
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看招募列表');

        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getIndex2  社会活动数据]
     * @return json
     * @author [C。]
     */
    public function getIndex2()
    {
        $param = input('param.');
        $user = $this->user;
        $map = [];
        $this->getPageAndSize($param);
        $model = new GatherModel;
        $rmodel = new GatherregModel;
        try{
            if(!empty($param['con'])){
                $map['title|name|phone'] = ['like', "%" . $param['con'] . "%"];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['starttime|endtime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $map['son_states'] = ['neq',-1];
            $map['comid'] = $user['comid'];
            $map['type'] = 1;
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size);
            foreach($lists as &$v){
                $regnum = $rmodel->getAllCount(['gather_id'=>$v['id']]);
                $v['starttime'] = date('Y-m-d',$v['starttime']);
                $v['endtime'] = date('Y-m-d',$v['endtime']);
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                $v['num'] = $regnum;
            }
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看招募列表');

        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getFahui  法会记录]
     * @return json
     * @author [C。]
     */
    public function getFahui()
    {
        $param = input('param.');
        $user = $this->user;
        $map = [];
        $this->getPageAndSize($param);
        $model = new GatherregModel;
        try{
            if(!empty($param['con'])){
                $map['m.username|m.tel'] = ['like', "%" . $param['con'] . "%"];
            }
            if(!empty($param['sex'])){
                $map['m.sex'] = ['like', "%" . $param['sex'] . "%"];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['gr.addtime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $map['gr.comid'] = $user['comid'];
            $map['g.type'] = 1;
            $map['gr.memid'] = $param['memid'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size);
            foreach($lists as &$v){
                $regnum = $model->getAllCount(['gather_id'=>$v['id']]);
                $v['starttime'] = date('Y-m-d',$v['starttime']);
                $v['endtime'] = date('Y-m-d',$v['endtime']);
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                $v['num'] = $regnum;
            }
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看法会记录');

        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getVolun  义工记录]
     * @return json
     * @author [C。]
     */
    public function getVolun()
    {
        $param = input('param.');
        $user = $this->user;
        $map = [];
        $this->getPageAndSize($param);
        $model = new GatherregModel;
        try{
            if(!empty($param['con'])){
                $map['m.username|m.tel'] = ['like', "%" . $param['con'] . "%"];
            }
            if(!empty($param['sex'])){
                $map['m.sex'] = ['like', "%" . $param['sex'] . "%"];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['gr.addtime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $map['gr.comid'] = $user['comid'];
            $map['g.type'] = 2;
            $map['gr.memid'] = $param['memid'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size);
            foreach($lists as &$v){
                $regnum = $model->getAllCount(['gather_id'=>$v['id']]);
                $v['starttime'] = date('Y-m-d',$v['starttime']);
                $v['endtime'] = date('Y-m-d',$v['endtime']);
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                $v['num'] = $regnum;
            }
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看义工记录');

        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getReg  查看报名人数]
     * @return json
     * @author [C。]
     */
    public function getReg1()
    {
        $param = input('param.');
        $user = $this->user;
        $map = [];
        $this->getPageAndSize($param);
        $model = new GatherregModel;
        try {
            if(!empty($param['con'])){
                $map['g.title|gr.name|gr.phone'] = ['like', "%" . $param['con'] . "%"];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['gr.addtime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }

            $map['gr.gather_id'] =  $param['id'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size);
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看招募人数');

        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getReg  查看报名人数]
     * @return json
     * @author [C。]
     */
    public function getReg2()
    {
        $param = input('param.');
        $user = $this->user;
        $map = [];
        $this->getPageAndSize($param);
        $model = new GatherregModel;
        try {
            if(!empty($param['con'])){
                $map['g.title|gr.name|gr.phone'] = ['like', "%" . $param['con'] . "%"];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['gr.addtime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }

            $map['gr.gather_id'] =  $param['id'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size);
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看招募人数');

        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getRecord  查看报名记录]
     * @return json
     * @author [C。]
     */
    public function getRecord1()
    {
        $param = input('param.');
        $user = $this->user;
        $map = [];
        $this->getPageAndSize($param);
        $model = new GatherregModel;
        try {
            if(!empty($param['con'])){
                $map['g.title|gr.name|gr.phone'] = ['like', "%" . $param['con'] . "%"];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['gr.addtime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $map['g.type'] = 2;
            $map['gr.comid'] = $user['comid'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size);
            foreach($lists as &$v){
                $v['starttime'] = date('Y-m-d H:i:s',$v['starttime']);
                $v['endtime'] = date('Y-m-d H:i:s',$v['endtime']);
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            }
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看招募人数');

        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getRecord  查看报名记录]
     * @return json
     * @author [C。]
     */
    public function getRecord2()
    {
        $param = input('param.');
        $user = $this->user;
        $map = [];
        $this->getPageAndSize($param);
        $model = new GatherregModel;
        try {
            if(!empty($param['con'])){
                $map['g.title|gr.name|gr.phone'] = ['like', "%" . $param['con'] . "%"];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['gr.addtime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $map['g.type'] = 1;
            $map['gr.comid'] = $user['comid'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size);
            foreach($lists as &$v){
                $v['starttime'] = date('Y-m-d H:i:s',$v['starttime']);
                $v['endtime'] = date('Y-m-d H:i:s',$v['endtime']);
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            }
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看招募人数');

        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postEdit  修改]
     * @return json
     * @author [C。]
     */
    public function postEdit1()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new GatherModel;
        try{
            $param['starttime'] = strtotime($param['starttime']);
            $param['endtime'] = strtotime($param['endtime']);
            $id = $model->editData($param);
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'修改活动'.$id);
        return admin_json(1,'修改成功');
    }
    /**
     * [postEdit  修改]
     * @return json
     * @author [C。]
     */
    public function postEdit2()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new GatherModel;
        try{
            $param['starttime'] = strtotime($param['starttime']);
            $param['endtime'] = strtotime($param['endtime']);
            $id = $model->editData($param);
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'修改活动'.$id);
        return admin_json(1,'修改成功');
    }
    /**
     * [postAdd  添加]
     * @return json
     * @author [C。]
     */
    public function postAdd1()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new GatherModel;
        try{
            $param['comid'] = $user['comid'];
            $param['addtime'] = time();
            $param['starttime'] = strtotime($param['starttime']);
            $param['endtime'] = strtotime($param['endtime']);
            $id = $model->addData($param);
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'添加活动'.$id);
        return admin_json(1,'添加成功');
    }
    /**
     * [postAdd  添加]
     * @return json
     * @author [C。]
     */
    public function postAdd2()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new GatherModel;
        try{
            $param['comid'] = $user['comid'];
            $param['addtime'] = time();
            $param['starttime'] = strtotime($param['starttime']);
            $param['endtime'] = strtotime($param['endtime']);
            $id = $model->addData($param);
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'添加活动'.$id);
        return admin_json(1,'添加成功');
    }
    /**
     * [getDel  删除]
     * @return json
     * @author [C。]
     */
    public function getDel1()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new GatherModel;
        try{
            $model->deleteFalseData(['id'=>$param['id']],['son_states'=>-1]);
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'删除招募活动'.$param['id']);
        return admin_json(1,'删除成功');
    }
    /**
     * [getDel  删除]
     * @return json
     * @author [C。]
     */
    public function getDel2()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new GatherModel;
        try{
            $model->deleteFalseData(['id'=>$param['id']],['son_states'=>-1]);
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'删除招募活动'.$param['id']);
        return admin_json(1,'删除成功');
    }
    /**
     * [postCouple  反馈]
     * @return json
     * @author [C。]
     */
    public function postCouple()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new GatherregModel;
        try{
            $reg = $model->findData($param['id'],'id','comid');
            if($reg['comid'] != $user['comid']){
                return admin_json(-1,'你想多了！');
            }
            $id = $model->editData($param);
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'报名反馈'.$id);
        return admin_json(1,'反馈成功');
    }
}